/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2020. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */

/*!
 * @file ac780x_timer.c
 *
 * @brief This file provides timer module integration functions.
 *
 */

/* ===========================================  Includes  =========================================== */
#include "ac780x_timer_reg.h"

/* ============================================  Define  ============================================ */

/* ===========================================  Typedef  ============================================ */

/* ==========================================  Variables  =========================================== */
/* TIMER related info */
static const IRQn_Type s_timerIRQ = TIMER_CHANNEL_IRQn;

/* TIMER callback pointer */
static DeviceCallback_Type s_timerCallback[TIMER_CHANNEL_MAX] = {NULL};

/* ====================================  Functions declaration  ===================================== */

/* ======================================  Functions define  ======================================== */

/*!
 * @brief TIMER module initialize.
 *
 * @param[in] timekclk:select timer clk source
 * @return none
 */
void TIMER_Init(TIMER_ClkSrcType timerclk)
{
    /* Enbale timer clock */
    CKGEN_Enable(CLK_TIMER, ENABLE);
    CKGEN_SoftReset(SRST_TIMER, ENABLE);

    /* Enbale timer module */
    TIMER_Enable(ENABLE);

    /* Select timer clk souce */
    TIMER_SetClockSelect(timerclk);
}

/*!
 * @brief TIMER channel initialize.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @param[in] config: pointer to configuration structure
 * @return none
 */
void TIMER_InitChannel(TIMER_CHANNEL_Type *TIMERCHx, const TIMER_ConfigType *config)
{
    uint8_t instance = 0;

    instance = TIMER_CHANNEL_INDEX(TIMERCHx);

    DEVICE_ASSERT(TIMER_CHANNEL_MAX > instance);
    DEVICE_ASSERT(NULL != config);

    /* Set timer channel mode */
    TIMER_SetChannelModeCmd(TIMERCHx, config->timeMode);

    /* Set capture pulse polarity mode */
    TIMER_SetPulsePolarity(TIMERCHx, config->pinPol);

    /* Set timer channel chain mode */
    TIMER_SetChainMode(TIMERCHx, config->chainModeEn);

    /* Set timer channel bypass enable */
    TIMER_SetBypass(TIMERCHx, config->bypEn);

    /* Set timer channel prescaler value */
    TIMER_SetPrescaler(TIMERCHx, config->prescaler);

    /* Set timer channel read cval precision selection */
    if (TIMER_WORKMODE_PULSECOUNTER == config->timeMode)
    {
        TIMER_SetSELValue(TIMERCHx, TIMER_BCLK_FCLK_4);
    }
    else
    {
        TIMER_SetSELValue(TIMERCHx, config->cvalReadSel);
    }

    /* Set timer channel period value */
    TIMER_SetPeriodLoadValue(TIMERCHx, config->periodValue);

    /* Register callback function */
    TIMER_SetCallback(TIMERCHx, config->callback);

    if (ENABLE == config->interruptEn)
    {
        /* Set timer channel interrupt */
        TIMER_EnableInterrupt((uint32_t)1U << instance);
        NVIC_EnableIRQ(s_timerIRQ);
    }
    else
    {
        /* clear timer channel interrupt */
        TIMER_DisableInterrupt((uint32_t)1U << instance);
    }

    /* Enable timer channel */
    TIMER_StartChannel((uint32_t)1U << instance);
}

/*!
 * @brief TIMER De-initialize module.
 *
 * close all channels at the same time
 *
 * @param[in] none
 * @return none
 */
void TIMER_DeInit(void)
{
    uint8_t i = 0;

    for (i = 0; i < TIMER_CHANNEL_MAX; i++)
    {
        s_timerCallback[i] = NULL;
        NVIC_ClearPendingIRQ(s_timerIRQ);
    }
    NVIC_DisableIRQ(s_timerIRQ);

    CKGEN_SoftReset(SRST_TIMER, DISABLE);
    CKGEN_Enable(CLK_TIMER, DISABLE);
}

/*!
 * @brief TIMER channel De-initialize.
 *
 * only close single channel
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @return none
 */
void TIMER_DeInitChannel(TIMER_CHANNEL_Type *TIMERCHx)
{
    uint8_t instance = 0;

    instance = TIMER_CHANNEL_INDEX(TIMERCHx);

    DEVICE_ASSERT(TIMER_CHANNEL_MAX > instance);
    s_timerCallback[instance] = NULL;
    TIMER_StopChannel((uint32_t)1U << instance);
    TIMER_DisableInterrupt((uint32_t)1U << instance);
    TIMER_SetPeriodLoadValue(TIMERCHx, 0);
    TIMER_SetChainMode(TIMERCHx, DISABLE);
    TIMER_ClearInterruptFlag(ALLCHANNEL_INTERRUPT_FLAG);
    NVIC_DisableIRQ(s_timerIRQ);
    NVIC_ClearPendingIRQ(s_timerIRQ);
}

/*!
 * @brief Set timer callback function.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 *            func: callback function
 * @return none
 */
void TIMER_SetCallback(TIMER_CHANNEL_Type *TIMERCHx, const DeviceCallback_Type func)
{
    uint8_t instance = 0;

    instance = TIMER_CHANNEL_INDEX(TIMERCHx);

    DEVICE_ASSERT(TIMER_CHANNEL_MAX > instance);
    s_timerCallback[instance] = func;
}

/*!
 * @brief TIMER common interrupt service routine.
 *
 * @param[in] TIMERCHx: timer channel
 *               - TIMER_CHANNEL0
 *               - TIMER_CHANNEL1
 *               - TIMER_CHANNEL2
 *               - TIMER_CHANNEL3
 * @return none
 */
static void TIMER_CommonISR(TIMER_CTRL_Type *TIMERCHx)
{
    uint32_t wpara = 0;

    /* store device status */
    wpara = TIMER_GetInterruptFlag(ALLCHANNEL_INTERRUPT_FLAG);
    if (wpara)
    {
        if (wpara & CHANNEL0_INTERRUPT_FLAG)
        {
            /* clear device status */
            TIMER_ClearInterruptFlag(CHANNEL0_INTERRUPT_FLAG);

            if (s_timerCallback[0] != NULL)
            {
                s_timerCallback[0](TIMERCHx, wpara, 0);
            }
        }
        if (wpara & CHANNEL1_INTERRUPT_FLAG)
        {
            /* clear device status */
            TIMER_ClearInterruptFlag(CHANNEL1_INTERRUPT_FLAG);

            if (s_timerCallback[1] != NULL)
            {
                s_timerCallback[1](TIMERCHx, wpara, 0);
            }
        }
        if (wpara & CHANNEL2_INTERRUPT_FLAG)
        {
            /* clear device status */
            TIMER_ClearInterruptFlag(CHANNEL2_INTERRUPT_FLAG);

            if (s_timerCallback[2] != NULL)
            {
                s_timerCallback[2](TIMERCHx, wpara, 0);
            }
        }
        if (wpara & CHANNEL3_INTERRUPT_FLAG)
        {
            /* clear device status */
            TIMER_ClearInterruptFlag(CHANNEL3_INTERRUPT_FLAG);

            if (s_timerCallback[3] != NULL)
            {
                s_timerCallback[3](TIMERCHx, wpara, 0);
            }
        }
    }
}

/*!
 * @brief TIMER channel interrupt request handler.
 *
 * @param[in] none
 * @return none
 */
void TIMER_Channel_IRQHandler(void)
{
    TIMER_CommonISR((TIMER_CTRL_Type *)TIMER_CTRL_BASE);
}

/* =============================================  EOF  ============================================== */
